package com.example.demo.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo.entity.Admin;
import com.example.demo.entity.AdminQuery;
import com.example.demo.entity.Student;
import com.example.demo.entity.common.CommonResult;
import com.example.demo.listener.StudentExcelListener;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;

/**
 * (Student)表控制层
 *
 * @author makejava
 * @since 2020-05-31 10:39:49
 */
@Controller
@RequestMapping("student")
public class StudentController {
    /**
     * 服务对象
     */
    @Resource
    @Autowired
    private StudentService studentService;


    @PostMapping("deleteById")
    @ResponseBody
    public boolean deleteById(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return false;
        }
        System.out.println(ids);
        studentService.deleteById(Arrays.asList(ids));
        return true;
    }

    @PostMapping("deleteById1")
    @ResponseBody
    public boolean deleteById1(Integer id) {
        System.out.println("删除单条学生数据");
        if ( id == null) {
            return false;
        }
        System.out.println(id);
        return studentService.deleteById1(id);
    }

    @PostMapping("queryAll")
    @ResponseBody
    public Object queryAll(Integer page, Integer limit, Student bean) {
        System.out.println(bean);
        CommonResult<Student> result = new CommonResult<>();
        IPage<Student> ipage = studentService.queryAllByLimit(page, limit, bean);
        result.setCode(0);
        result.setCount(ipage.getTotal());
        result.setData(ipage.getRecords());
        System.out.println("学生数据查询成功");
        System.out.println(result);
        return result;
    }

    @RequestMapping("save")
    @ResponseBody
    public Object save(Student bean) {
        System.out.println("修改和添加学生数据");
        boolean result = false;
        //判断账号是否存在
        //判断是添加还是修改
        if (bean.getId() != null) {
            result = studentService.update(bean) > 0;
        } else {
            result = studentService.insert(bean).getId() != null;
        }
        return result;
    }

    @RequestMapping("toList")
    public String toList() {
        return "user/student_list";
    }

    @RequestMapping("toEdit")
    public String toEdit(Integer id,Model model){
        Student bean = studentService.queryById(id);
        model.addAttribute("bean",bean);
        return "user/student_add";
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model){
        model.addAttribute("bean",new Student());
        return "user/student_add";
    }

    /**
     * 文件上传
     */
    @RequestMapping("upload")
    @ResponseBody
    public Object upload(MultipartFile file) {
        CommonResult<String> result = new CommonResult<>();
        try {
            EasyExcel.read(file.getInputStream(), Student.class, new StudentExcelListener(studentService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            result.setMsg("文件上传异常");
        }
        result.setData(file.getOriginalFilename());
        return result;
    }

}